<template>
	<view class="all-main">
		<view class="main">
			<view class="con">
				<view class="title">
					<text>{{info.title}}</text>
				</view>
				<view class="content">
					<text v-html="info.content"></text>
				</view>
			</view>
			<view class="btn" @click="participation">
				<text>参与活动</text>
			</view>
			<u-overlay :show="show" @click="show = false">
				<view class="sn-list" @tap.stop>
					<view class="sn-title">
						<text>参与活动</text>
					</view>
					<view class="sn-introduce">
						<text v-html="info.introduce"></text>
					</view>
					<view class="sn-item">
						<view class="sn-titles border-blue">
							<text>好展业</text>
						</view>
						<view class="sn-con" @click="selectSn(1)">
							<view class="lefts">
								<text>{{hzy_sn}}</text>
							</view>
							<view class="rights">
								<u-icon name="search" color="#2979ff" size="28"></u-icon>
							</view>
						</view>
					</view>
					<view class="sn-item">
						<view class="sn-titles border-warm">
							<text>燎原</text>
						</view>
						<view class="sn-con" @click="selectSn(2)">
							<view class="lefts">
								<text>{{ly_sn}}</text>
							</view>
							<view class="rights">
								<u-icon name="search" color="#2979ff" size="28"></u-icon>
							</view>
						</view>
					</view>
					<view class="sn-btn" @click="participationFun">
						<text>参与活动</text>
					</view>
				</view>
			</u-overlay>
			<u-picker :show="sn_show" :columns="sn_list" @cancel="sn_show = false" @confirm="confirm"></u-picker>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				info: {},
				show: false,
				ly_sn: '请选择终端',
				hzy_sn: '请选择终端',
				sn_show: false,
				sn_list: [],
				hzySn: [],
				lySn: [],
				type: 1,
				is_submit: true
			}
		},
		onLoad(e) {
			this.id = e.id
			this.loadData()
			this.getSnList()
		},
		methods: {
			// 加载数据
			async loadData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getActivityDetail',
					data: {
						id: this.id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.info = res.data
				}
			},
			// 获取终端
			async getSnList() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getSnList'
				});
				if (res.code == 200) {
					this.hzySn = res.data.hzySn
					this.lySn = res.data.lySn
				}
			},
			// 参与活动
			async participation() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/participationVerify',
					data: {
						id: this.id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.show = true
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
				}
			},
			// 选择SN
			selectSn(type) {
				this.type = type
				if (type == 1) {
					this.sn_list = [this.hzySn]
				} else {
					this.sn_list = [this.lySn]
				}
				this.sn_show = true
			},
			// 确认选择
			confirm(e) {
				if (e.value[0] != undefined) {
					if (this.type == 1) {
						this.hzy_sn = e.value[0]
					} else {
						this.ly_sn = e.value[0]
					}
				}
				this.sn_show = false
			},
			// 确认参与活动
			async participationFun() {
				if (this.hzy_sn == '' || this.hzy_sn == '请选择终端') {
					uni.showToast({
						title: '请选择好展业终端！',
						icon: 'none',
						duration: 1000
					})
					return false
				}
				if (this.ly_sn == '' || this.ly_sn == '请选择终端') {
					uni.showToast({
						title: '请选择燎原终端！',
						icon: 'none',
						duration: 1000
					})
					return false
				}
				uni.showLoading({
					title: '加载中'
				});
				if (this.is_submit) {
					this.is_submit = false
					const {
						data: res
					} = await this.$myHttp({
						url: 'v1/participationFun',
						data: {
							id: this.id,
							hzy_sn: this.hzy_sn,
							ly_sn: this.ly_sn
						}
					});
					this.is_submit = true
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
					if (res.code == 200) {
						this.show = false
						setTimeout(function() {
							uni.navigateBack({});
						}, 1000)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 30rpx;

		.con {
			background-color: #fff;
			padding: 30rpx;

			.title {
				font-size: 34rpx;
				font-weight: bold;
				padding-bottom: 30rpx;
				text-align: center;
				border-bottom: 1px dashed #c8c9cc;
			}

			.content {
				padding-top: 30rpx;
			}
		}

		.sn-list {
			margin: 0 auto;
			margin-top: 50%;
			width: 80%;
			background-color: #fff;

			.sn-title {
				padding-bottom: 20rpx;
				font-size: 35rpx;
				font-weight: bold;
				text-align: center;
				padding: 20rpx 0;
				border-bottom: 1px solid #c8c9cc;
			}

			.sn-introduce {
				font-size: 28rpx;
				color: #fa3534;
				padding: 20rpx;
			}

			.sn-item {
				margin: 0 20rpx;
				margin-bottom: 30rpx;

				.sn-titles {
					padding: 10rpx 0;
					padding-left: 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
				
				.border-blue {
					border-left: 5px solid #2979ff;
				}
				
				.border-warm {
					border-left: 5px solid #fa3534;
				}

				.sn-con {
					margin-top: 20rpx;
					display: flex;
					justify-content: space-between;
					border: 1px solid #c8c9cc;
					border-radius: 8rpx;
					padding: 20rpx;
				}
			}
		
			.sn-btn {
				padding: 20rpx;
				background-color: #2979ff;
				text-align: center;
				border-radius: 10rpx;
				color: #fff;
				margin: 0 20rpx;
				margin-top: 50rpx;
				margin-bottom: 30rpx;
			}
		}

		.btn {
			padding: 20rpx 30rpx;
			background-color: #2979ff;
			text-align: center;
			color: #fff;
			font-weight: bold;
			margin-top: 30rpx;
			border-radius: 10rpx;
		}
	}
</style>